<template>
  <div>
    <van-form @submit="login">
      <van-field
        v-model="user.phone"
        name="账号"
        label="账号"
        placeholder="账号"
        :rules="[{ required: true, message: '请填写账号' }]"
      />
      <van-field
        v-model="user.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="danger" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import {Toast} from "vant"
import { reqlogin } from "../../http/http";
import axios from "axios";
export default {
  data() {
    return {
      user: {
        phone: "",
        password: ""
      }
    };
  },
  methods: {
    //点击登陆
    login() {
      //发请求
      reqlogin(this.user).then(res => {
        console.log("交互成功");
        if (res.data.code === 200) {
          //登陆成功
          //1.1弹成功
          Toast(res.data.msg);
          //1.2存判断是否登陆的标识，而这个标识我们存了用户的信息
          localStorage.setItem("isLogin", JSON.stringify(res.data.list));
          //1.3跳转到首页
          this.$router.push("/index/home");
        } else {
          //登陆失败
          Toast(res.data.msg);
        }
      });
    }
  }
};
</script>

<style scoped>
div {
  font-size: 0.2rem;
}
</style>